/*=========================================================================================================================================

					
					USAGE IN			:		VFS PROJECT, GROUP 31
					
					OPERATION TYPE		:		FILE OPERATION
					
					NAME OF FILE		:		list_file.c
					
					NAME OF OPERATION	:		listfile(P1, P2)
					
												- P1: source file path (in VFS)
												- P2: output file path (in Hard Disk)
					
					FORMALY CALLED AS	:		listfile_31(source_file_path, output_file_path)
					
					VERSION				:		1.0
					
					CREATED BY			:		ANUJ DUGGAL
					
					TESTED BY			:		ANUJ DUGGAL
					
					
					TODO				:		Define a function that accepts two arguments as mentioned above, and does the following:
												
												VALIDATIONS (APPLYING ERROR CODES):
													- mounted, 
													- source file existance (in VFS),
													- output file existance (in Hard Disk), 
													- output file is a text file
													
												TASK:
													- Search the source file in N-ARY
													- Read the contents of the file (File which is in VFS)
													- List (write) the contents read above into an output file (on Hard disk)
													
					OTHER FUNCTIONS		:		- validateMountState()
					CALLED						- search_nary(file_path)
												- validateFileExistance(output_file)
												- validateTextFile(name)
												- search(bst_root, file_path)


==========================================================================================================================================*/


#include"common_headers.h"
int listfile31(char file_path[],char output_file[])
{
	//DECLARATIONS:
	int is_mounted, is_source_file_exist, is_output_file_exist, bno, collect, is_text_file;
	struct BST_Node *temp;
	struct Block block;
	char output_file_path[300], name[30], filenameFromfilepath[30], source_file_path[300];
	char  delims[]="/";
	char *result;
	FILE *fptr;
	
	//INITIALIZATIONS:
	is_mounted=0;
	is_source_file_exist=0;
	is_output_file_exist=0;
	bno=0;
	collect=0;
	is_text_file=0;
	temp=NULL;
	fptr=NULL;
	result=NULL;

	//CHECK IF VFS MOUNTED
	is_mounted = validateMountState();
	if(is_mounted)
	{
		//CHECK SOURCE FILE EXISTANCE (DONE IN VFS)
		is_source_file_exist = search_nary(file_path);		
		if(is_source_file_exist)
		{		
			strcpy(source_file_path,file_path);
				
			//EXTRACTING FILENAME FROM SOURCE FILE PATH					
			result = strtok(source_file_path, delims);
			while(result!=NULL)
			{
				strcpy(filenameFromfilepath, result);
				result=strtok(NULL, delims);
			}
			strcpy(name,filenameFromfilepath);	//FILENAME EXTRACTED IN "name" VARIABLE
						
			//CHECK IF SOURCE FILE NAME IS A TEXT FILE
			is_text_file = validateTextFile(name);			
			if(is_text_file)
			{		
				//CHECK DESTINATION FILE EXISTANCE (HARD DISK PATH)
				is_output_file_exist = validateFileExistance(output_file);	
				if(is_output_file_exist)
				{					
						//FILE MENTIONED IS A TEXT FILE, AND EXIST IN THE HARD DISK
					
						//SEARCHING THE FILE IN BST BASED ON PATH		
						temp=search(bst_root, file_path);
					
						//EXTRACTING THE LOCATION BLOCK NUMBER 
						bno= temp->data->locationblocknumber;  
						          
						fptr=fopen(mainHeader.filelabel,"rb");
						
						//SEEKING TILL THE LAST WRITE POSITION OF THE FILE
						fseek(fptr,(sizeof(struct MainHeader)+(mainHeader.maximumFileDescriptor*sizeof(char))+(mainHeader.maximumFileDescriptor*sizeof(struct FileDescriptor))+(bno*sizeof(struct Block))),SEEK_SET);
						
						//READING THE CONTENTS OF FILE WHICH IS IN VFS
						fread(&block,sizeof(struct Block),1,fptr);
						fclose(fptr);           
						
						fptr=fopen(output_file,"w");
						
						//WRITING THE CONTENTS THE FILE READ FROM VFS IN THE FILE ON HARD DISK
						fwrite(block.block,strlen(block.block),1,fptr);
						fclose(fptr);
						collect=1;
						printf("listfile_SUCCESS\n");													
				}
				else
				{
					//OUTPUT_FILE_PATH_NOT_FOUND
					printf("listfile_FAILURE %s\n", ERR_VFS_LISTFILE_02);	
				}
			}
			else
			{
				//NOT_A_TEXT_FILE
				printf("listfile_FAILURE %s\n", ERR_VFS_LISTFILE_05);	
			}	
		}
		else
		{
			//SOURCE_FILE_PATH_NOT_FOUND
			printf("listfile_FAILURE %s\n", ERR_VFS_LISTFILE_01);		
		}			
	}
	else
	{
		//VFS_NOT_MOUNTED
		printf("listfile_FAILURE %s\n", ERR_VFS_LISTFILE_06);	
	}
    return collect;
    
}
